//index.js
//获取应用实例 
let util = require('../../utils/util');
const api = require('../../utils/api');
Page({
  data: {
    
    produ: [],
    curType: 0,
    imgUrls: [],
    frontName: '',
    contentData: "",
  },
  //一级菜单
  click(e) {
    // console.log(e)
    // let that = this;
    // that.setData({
    //   curType: e.currentTarget.dataset.index
    // })
    let that = this;
    console.log(e);
    let id = e.currentTarget.dataset.id;
    //e.currentTarget.dataset.id 的id是个名字，这个名字必须和data-id的id名字一样，把data-id里面的什么值赋值给他 ，他就是什么值，本篇是把item的id赋值给力他，他就是item里面的id，item是for循环固有的，data-id的功能是唯一标识
    that.setData({
      curType: id
    })

    this.goodsList(id);
  },
  //点击图片跳转至商品详情页
  clickImg(e) {
    console.log(2222,e)
    wx.navigateTo({
      url: '../../pages/productInfo/productInfo?id='+e.currentTarget.dataset.id,
    })
     
  },
  //点击二级菜单
  onTabSub(e) {
    let that = this;
    // console.log(e.currentTarget.dataset.index)
    wx.showLoading({
      title: '加载中！',
    })
    util.myRequest(
      api.goodsList, 
      {
        categoryId: e.currentTarget.dataset.index
      },
      res => {
        // console.log(111, res)
        that.setData({
          contentData: res.data.data
        })
        wx.hideLoading({ title: "加载完成！" })
        if (res.data.data.count == 0) {
          wx.showToast({
            title: '暂无数据',
            icon:"none"
          })
        }
      }
    )
  },
  onLoad: function() {
    let that = this;
    //loading动画
    // wx.showNavigationBarLoading();
    // wx.showLoading({
    //   title: '玩命加载中',
    //   mask: true
    // })



    //二级菜单点开后的商品列表
    util.myRequest(
      api.productType, {},
      res => {
        // console.log(0, res);
        that.setData({
          produ: res.data.data,
        })
        // let imgs = that.data.produ.categoryList[0];
        that.setData({
          imgUrls: res.data.data.currentCategory
        })
        // console.log(that.data.imgUrls.subCategoryList)
        //默认显示
        util.myRequest(
          api.goodsList, {
            categoryId: res.data.data.currentCategory.subCategoryList[0].id
          },
          res => {
            // console.log(res)

            that.setData({
              contentData: res.data.data
            })
          }
        )
        wx.hideLoading({title:"加载完成！"})
      },

    )
    
  },
  goodsList(id) {
    let that = this;
    util.myRequest(
      api.productType, {
        id: id
      },
      res => {
        that.setData({
          imgUrls: res.data.data.currentCategory
        })
        console.log(1111, res.data.data.currentCategory);
        
      }
    )
  }

})